package com.hellogood.yaohuo.client.common;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hellogood.yaohuo.shared.entity.Goods;
import com.hellogood.yaohuo.shared.services.GoodsService;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.types.Positioning;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.CloseClickEvent;
import com.smartgwt.client.widgets.events.CloseClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.fields.events.KeyPressEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyPressHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class GoodsSearchWindow extends Window {
	private ListGrid listGrid = null;
	private DynamicForm form = null;
	private String code = null;

	private TextItem keyItem = null;
	private ListGrid searchResultListGrid = null;
	private BasicVoSearchInterface basicVoSearchInterface;
	
	public static boolean isOpen = false;

	public GoodsSearchWindow(BasicVoSearchInterface basicVoInterface, ListGrid listGrid, String code) {
		isOpen = true;
		this.basicVoSearchInterface = basicVoInterface;
		this.code = code;
		this.listGrid = listGrid;
		setIsModal(true);
		setShowModalMask(true);
		initData();
		initView();
		setTitle("商品查询");
		setPosition(Positioning.RELATIVE);
		addCloseClickHandler(new CloseClickHandler() {

			@Override
			public void onCloseClick(CloseClickEvent event) {
				destroy();
				isOpen = false;
			}
		});

	}

	private void initData() {
		if (!code.equals("")) {
			GoodsService.Util.getInstance().searchGoodsList(code, new SearchAsyncCallback());
		}
	}

	private void initView() {
		keyItem = new TextItem("key", "请输入条码、编码、助记符查询");
		keyItem.setRequired(true);
		keyItem.setValue(code);
		keyItem.setTitleColSpan(3);
		keyItem.setColSpan(1);
		keyItem.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				if (event.getKeyName().equals("Enter")) {
					if (form.validate()) {
						Object key = keyItem.getValue();
						if (key != null) {
							GoodsService.Util.getInstance().searchGoodsList(key.toString(), new SearchAsyncCallback());
						}
					}
				}
			}
		});
		ButtonItem searchItem = new ButtonItem("searchBtn", "查询");
		searchItem.setStartRow(false);
		searchItem.addClickHandler(new SearchClickHandler());

		form = new DynamicForm();
		form.setSize("100%", "10%");
		form.setFields(keyItem, searchItem);
		form.setNumCols(6);
		form.setMargin(10);

		ListGridField pluCodeField = new ListGridField("pluCode", "编码");
		pluCodeField.setWidth("20%");
		ListGridField barCodeField = new ListGridField("barCode", "条码");
		barCodeField.setWidth("20%");
		ListGridField pluNameField = new ListGridField("pluName", "商品名");
		pluNameField.setWidth("40%");
		ListGridField pluMNField = new ListGridField("pluMN", "助记符");
		pluMNField.setWidth("20%");

		searchResultListGrid = new ListGrid();
		searchResultListGrid.setCanFocus(true);
		searchResultListGrid.setFields(pluCodeField, barCodeField, pluNameField, pluMNField);
		searchResultListGrid.setSize("100%", "90%");
		searchResultListGrid.setGenerateDoubleClickOnEnter(true);
		searchResultListGrid.addRecordDoubleClickHandler(new SearchResultListDoubleClickHandler());
		searchResultListGrid.setEmptyMessage("");		
		
		VLayout layout = new VLayout();
		layout.setMembers(form, searchResultListGrid);
		layout.setMembersMargin(10);
		layout.setTop(20);
		layout.setSize("100%", "95%");
		addChild(layout);
		if (code.toString().equals("")) {
			keyItem.focusInItem();
		}
	}

	public TextItem getKeyItem() {
		return keyItem;
	}

	public ListGrid getListGrid() {
		return searchResultListGrid;
	}

	private class SearchClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			if (form.validate()) {
				Object key = keyItem.getValue();
				if (key != null) {
					GoodsService.Util.getInstance().searchGoodsList(key.toString(), new SearchAsyncCallback());
				}
			}
		}
	}

	private class SearchAsyncCallback implements AsyncCallback<List<Goods>> {

		@Override
		public void onFailure(Throwable caught) {
			SC.say(caught.getMessage());
		}

		@Override
		public void onSuccess(List<Goods> result) {
			if (result != null) {
				Record[] rds = new Record[result.size()];
				for (int i = 0; i < result.size(); i++) {
					Goods g = result.get(i);
					Record r = new Record();
					r.setAttribute("pluCode", g.getPluCode());
					r.setAttribute("pluName", g.getPluName());
					r.setAttribute("pluMN", g.getPluMN());
					r.setAttribute("barCode", g.getBarCode());
					rds[i] = r;
				}
				searchResultListGrid.setData(rds);
				if (result.size() > 0) {
					searchResultListGrid.selectSingleRecord(0);
					searchResultListGrid.focus();
				} else {
					keyItem.focusInItem();
					keyItem.selectValue();
					SC.say("无数据");
				}
			} else {
				SC.say("无数据");
			}
		}
	}

	private class SearchResultListDoubleClickHandler implements RecordDoubleClickHandler {

		@Override
		public void onRecordDoubleClick(RecordDoubleClickEvent event) {
			Record resultRecord = searchResultListGrid.getSelectedRecord();
			if (resultRecord != null) {
				listGrid.setEditValue(listGrid.getEditRow(), 1, resultRecord.getAttribute("pluCode"));
				listGrid.startEditing(listGrid.getEditRow(), 1, true);				
				basicVoSearchInterface.fillRowWithBasicVo();
				GoodsSearchWindow.this.setVisible(false);
				GoodsSearchWindow.this.destroy();
				isOpen = false;
			}
		}
	}

}
